package org.dromara.business.controller;

import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.business.domain.BusinessCollect;
import org.dromara.business.domain.BusinessInformation;
import org.dromara.business.domain.User;
import org.dromara.business.domain.bo.BusinessCollectBo;
import org.dromara.business.domain.vo.BusinessCollectVo;
import org.dromara.business.domain.vo.BusinessInformationVo;
import org.dromara.business.domain.vo.UserVo;
import org.dromara.business.mapper.BusinessCollectMapper;
import org.dromara.business.mapper.BusinessInformationMapper;
import org.dromara.business.service.BusinessCollectService;
import org.dromara.business.service.BusinessInformationService;
import org.dromara.business.service.UserService;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.vo.SysUserVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户收藏
 *
 * @author zlj
 * @date 2024-11-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/collect")
public class BusinessCollectController extends BaseController {
    private final UserService userService;
    private final BusinessCollectService businessCollectService;
    private final BusinessCollectMapper businessCollectMapper;
    private final BusinessInformationService businessInformationService;
    private final BusinessInformationMapper businessInformationMapper;

    /**
     * 查询用户收藏列表
     */
    @GetMapping("/list")
    public TableDataInfo<BusinessInformationVo> list(BusinessCollectBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        User user = userService.getById(userId);


        LambdaQueryWrapper<BusinessCollect> lambdaQueryWrapper = new LambdaQueryWrapper<BusinessCollect>();
        lambdaQueryWrapper.eq(BusinessCollect::getUserId, user.getUserId());
        //IPage<BusinessCollect> page = pageQuery.build();
        //IPage<BusinessCollectVo> voIPage = businessCollectMapper.selectVoPage(page,lambdaQueryWrapper);

        List<BusinessCollect> cList = businessCollectMapper.selectList(lambdaQueryWrapper);
        List<Long> ids = cList.stream().map(BusinessCollect::getUserIdPassivity).toList();

        IPage<BusinessInformationVo> voIPage = null;
        if (ids!= null && ids.size()>0){
            LambdaQueryWrapper<BusinessInformation> lwInformList = new LambdaQueryWrapper<BusinessInformation>();
            lwInformList.in(BusinessInformation::getId, ids);
            IPage<BusinessInformation> page = pageQuery.build();
            voIPage = businessInformationMapper.selectVoPage(page,lwInformList);

            for (BusinessInformationVo vo : voIPage.getRecords()){
                vo.setSfxh(true);
            }
        }else {
            return TableDataInfo.build();
        }


        return TableDataInfo.build(voIPage);

    }

    /**
     * 导出用户收藏列表
     */
    @Log(title = "用户收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusinessCollectBo bo, HttpServletResponse response) {
        List<BusinessCollectVo> list = businessCollectService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户收藏", BusinessCollectVo.class, response);
    }

    /**
     * 获取用户收藏详细信息
     *
     * @param businessCollectId 主键
     */
    @GetMapping("/{businessCollectId}")
    public R<BusinessCollectVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long businessCollectId) {
        return R.ok(businessCollectService.queryById(businessCollectId));
    }

    /**
     * 新增用户收藏
     */
    @Log(title = "用户收藏", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@RequestBody BusinessCollectBo bo) {
        Long userId = LoginHelper.getUserId();
        User user = userService.getById(userId);
        bo.setUserId(user.getUserId());
        return toAjax(businessCollectService.insertByBo(bo));
    }

    /**
     * 修改用户收藏
     */
    @Log(title = "用户收藏", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@RequestBody BusinessCollectBo bo) {
        return toAjax(businessCollectService.updateByBo(bo));
    }

    /**
     * 删除用户收藏
     * @param inFormationId 主键串
     */
    @Log(title = "用户收藏", businessType = BusinessType.DELETE)
    @GetMapping("/delete")
    public R<Void> remove(String inFormationId) {
        Long userId = LoginHelper.getUserId();
        User user = userService.getById(userId);
        // 根据信息id和收藏人删除
        UpdateWrapper<BusinessCollect> UpdateWrapper = new UpdateWrapper<BusinessCollect>();
        UpdateWrapper.eq("user_id", userId);
        UpdateWrapper.eq("user_id_passivity", inFormationId);
        UpdateWrapper.set("del_flag","2");
        return toAjax(
            businessCollectService.update(UpdateWrapper)
        );
    }



}
